package me.xuym.crawler.house58;

import com.cbs.java.component.application.Application;
import com.cbs.java.component.application.BaseArgs;
import me.xuym.crawler.Processor;
import me.xuym.crawler.Task;
import me.xuym.crawler.simpleextension.dispatcher.SimpleDebugListDispatcher;
import me.xuym.crawler.simpleextension.filter.SimpleCachedMySQLListFilter;
import me.xuym.crawler.simpleextension.loader.SimpleWebPageLoader;

/**
 * Created by broche on 07/09/2017.
 */
public class MyApplication extends Application {
    private BaseArgs baseArgs = new BaseArgs();

    public MyApplication(String[] args) {
        super(args);
    }

    @Override
    public boolean isDebug() {
        return false;
    }

    @Override
    public String applicationName() {
        return "crawler/house58";
    }

    @Override
    public BaseArgs getArgs() {
        return baseArgs;
    }

    @Override
    public void start() {
        super.start();
        try {
            String Url = "http://cz.58.com/ershoufang/";
            String DBHost = getConfigs().getProperty("DBHost", "");
            String DBPort = getConfigs().getProperty("DBPort", "");
            String DBUsername = getConfigs().getProperty("DBUsername", "");
            String DBPassword = getConfigs().getProperty("DBPassword", "");
            String DBDatabase = getConfigs().getProperty("DBDatabase", "");
            String DBTable = "house58";
            String DBTable2 = "house58address";


            Task task = new Task(this);
            Processor processor = task.process(new SimpleWebPageLoader(this, Url))
                    .process(new House58Convertor(this))
                    .process(new SimpleCachedMySQLListFilter(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, DBTable));

            if (isDebug()) {
                processor.process(new SimpleDebugListDispatcher(this));
            }

            processor = task.process(new House58DeltaAddressLoader(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase))
                    .process(new SimpleCachedMySQLListFilter(this, DBHost, DBPort, DBUsername, DBPassword, DBDatabase, DBTable2));

            if (isDebug()) {
                processor.process(new SimpleDebugListDispatcher(this));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyApplication application = new MyApplication(args);
        application.start();
    }
}
